import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBFindModule/YBMainPage/YBNewFindMainPage.dart';

import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBHomePage/YBMainPage/YBHomePageAd.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBHomePage/YBMapMiShuPage/YBMapMiShuMainPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBWorkTaiModule/YBMainPage/YBWorkTaiMainPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineMainPage/YBMinePage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/yb_wenshu_main/yb_wenshu_main_page.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBToastUtils.dart';


class YBTabBarMainPage extends StatefulWidget {
  @override
  _YBTabBarMainPageState createState() => _YBTabBarMainPageState();
}

class _YBTabBarMainPageState extends State<YBTabBarMainPage> {
  int _tabIndex = 0;
  var tabImages;
  //,'文书'
  var appBarTitles = ['首页','工作台','发现','我的'];
  var currentPage;
  DateTime lastPopTime;



  /*
  * 根据选择获得对应的normal或是press的icon
  * */
  SvgPicture getTabIcon(int curIndex){
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }  else{
      return tabImages[curIndex][0];
    }
  }

  /*
  * 获取BottomTab的颜色和文字
  * */
  Text getTabTitle(int curIndex){
    if (curIndex == _tabIndex) {
       return Text(appBarTitles[curIndex],style:YBTextStyle.tabBarSelectedText,);
    }else{
      return Text(appBarTitles[curIndex],style:YBTextStyle.tabBarUnselectText,);
    }
  }

  SvgPicture getTabImage(path){
    return SvgPicture.asset(YBImageStyle.getTabbbarSVGPath(path));
  }

  void initData(){
    tabImages = [
      [
        getTabImage('bar_tabbar_icon_normal_home'),
        getTabImage('bar_tabbar_icon_select_home'),
      ],
      // [
      //   getTabImage('bar_tabbar_icon_normal_wenshu'),
      //   getTabImage('bar_tabbar_icon_select_wenshu'),
      // ],
      [
        getTabImage('bar_tabbar_icon_normal_shangji'),
        getTabImage('bar_tabbar_icon_select_shangji'),
      ],
      [
        getTabImage('bar_tabbar_icon_normal_message'),
        getTabImage('bar_tabbar_icon_select_message'),
      ],
      [
        getTabImage('bar_tabbar_icon_normal_mine'),
        getTabImage('bar_tabbar_icon_select_mine'),
      ],

    ];
  }

  // YBMapMiShuMainPage(),
  final List<Widget> tabBodies = [
    YBMainPage(),
  //  YBWenShuMainPage(),
    YBWorkTaiMainPage(),
    YBNewFindMainPage(),
    YBMineMainPage(),
  ];

  //BottomNavigationBarItem(icon: getTabIcon(1),title: getTabTitle(1)),
  @override
  Widget build(BuildContext context) {

    initData();
      //
    final List<BottomNavigationBarItem> bottomTabs = [
      BottomNavigationBarItem(icon: getTabIcon(0),title: getTabTitle(0)),
     // BottomNavigationBarItem(icon: getTabIcon(1),title: getTabTitle(1)),
      BottomNavigationBarItem(icon: getTabIcon(1),title: getTabTitle(1)),
      BottomNavigationBarItem(icon: getTabIcon(2),title: getTabTitle(2)),
      BottomNavigationBarItem(icon: getTabIcon(3),title: getTabTitle(3)),
    ];

    return SafeArea(
      child: WillPopScope(
          child: Scaffold(
            backgroundColor: YBColor.white,
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              elevation: 0,
              selectedFontSize: YBTextStyle.adapterSize12,
              unselectedFontSize: YBTextStyle.adapterSize12,
              currentIndex: _tabIndex,
              items:bottomTabs,
              onTap: (index) async {
                setState(() {
                  _tabIndex = index;
                  currentPage = tabBodies[_tabIndex];
                });
              },
            ),
            body: IndexedStack(
              index: _tabIndex,
              children: tabBodies,
            ),
          ),
          onWillPop:(){
            if (lastPopTime == null || DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
              lastPopTime = DateTime.now();
              YBToast.toast(context, "再按一次退出应用");
              return;
            } else{
              lastPopTime = DateTime.now();
              //退出APP
              SystemChannels.platform.invokeMethod("SystemNavigator.pop");
              return;
            }
        }
      ),
    );
  }
}

